﻿using System;
using System.Configuration;
using System.IO;
using System.Net.Mail;
using System.Text;

namespace Common
{
	public class WebMail
	{
		/// <summary>
		/// 发送邮件(无附件)
		/// </summary>
		/// <param name="userName"></param>
		/// <param name="passWord"></param>
		/// <param name="from"></param>
		/// <param name="tos"></param>
		/// <param name="ccs"></param>
		/// <param name="subject"></param>
		/// <param name="body"></param>
		/// <returns>是否发送成功</returns>
        public bool SendMail(string hostname, int postport, string userName, string passWord, string from, string[] tos, string[] ccs, string subject, string body)
		{
            return SendMail(hostname, postport, userName, passWord, from, tos, ccs, subject, body, null);
		}
		/// <summary>
		/// 发送邮件(带附件)
		/// </summary>
		/// <param name="userName"></param>
		/// <param name="passWord"></param>
		/// <param name="from"></param>
		/// <param name="tos"></param>
		/// <param name="ccs"></param>
		/// <param name="subject"></param>
		/// <param name="body"></param>
		/// <param name="attechments"></param>
		/// <returns>是否发送成功</returns>
        public bool SendMail(string hostname, int postport, string userName, string passWord, string from, string[] tos, string[] ccs, string subject, string body, string[] attechments)
		{
            return SendMail(hostname, postport, userName, passWord, from, tos, ccs, null, subject, body, attechments);
		}
		/// <summary>
		/// 发送邮件
		/// </summary>
		/// <param name="userName"></param>
		/// <param name="passWord"></param>
		/// <param name="from"></param>
		/// <param name="tos"></param>
		/// <param name="ccs"></param>
		/// <param name="bccs"></param>
		/// <param name="subject"></param>
		/// <param name="body"></param>
		/// <param name="attechments"></param>
		/// <returns>是否发送成功</returns>
        public bool SendMail(string hostname, int postport, string userName, string passWord, string from, string[] tos, string[] ccs, string[] bccs, string subject, string body, string[] attechments)
		{
			try
			{
			
				MailMessage mail = new MailMessage();

				//设置邮件的标题
				mail.Subject = subject ?? String.Empty;

				//设置邮件的发件人
				mail.From = new MailAddress(from);

				//收件人
				foreach (string to in tos)
				{
					if (!String.IsNullOrEmpty(to) && to.Trim().Length > 0)
					{
						mail.To.Add(new MailAddress(to.Trim()));
					}
				}
				//cc
				if (ccs != null)
				{
					foreach (string cc in ccs)
					{
						if (!String.IsNullOrEmpty(cc) && cc.Trim().Length > 0)
						{
							mail.CC.Add(new MailAddress(cc.Trim()));
						}
					}
				}
				//bcc
				if (bccs != null)
				{
					foreach (string bcc in bccs)
					{
						if (!String.IsNullOrEmpty(bcc) && bcc.Trim().Length > 0)
						{
							mail.Bcc.Add(new MailAddress(bcc.Trim()));
						}
					}
				}
				//附件
				if (attechments != null)
				{
					foreach (string attechment in attechments)
					{
						if (File.Exists(attechment))
						{
							mail.Attachments.Add(new Attachment(attechment));
						}
					}
				}
				//邮件内容
				mail.Body = body ?? String.Empty;

				mail.BodyEncoding = Encoding.UTF8;
				mail.IsBodyHtml = true;
				//设置邮件的发送级别
				mail.Priority = MailPriority.Normal;

				//mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
			    string post = hostname;
			    int host = postport;
				SmtpClient client = new SmtpClient();
				//设置用于 SMTP 事务的主机的名称，填IP地址也可以了
				client.Host = post;
				//设置用于 SMTP 事务的端口，默认的是 25
				client.Port = host;
				client.UseDefaultCredentials = false;
				//这里才是真正的邮箱登陆名和密码，比如我的邮箱地址是 hbgx@hotmail， 我的用户名为 hbgx ，我的密码是 xgbh
				client.Credentials = new System.Net.NetworkCredential(userName, passWord);
				client.DeliveryMethod = SmtpDeliveryMethod.Network;

				client.Send(mail);
				return true;
			}
			catch (Exception )
			{
				//throw new ExceptionBusiness(ex.Message);
				return false;
			}

		}
	}
}

